﻿using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;

public class LoadingPanel : MonoBehaviour
{
    private static LoadingPanel instance;

    public static LoadingPanel Instance
    {
        get
        {
            if(instance == null)
            {
                GameObject obj = Instantiate(Resources.Load("UI/Loading")) as GameObject;
                var uiroot = GameObject.Find("UI Root");
                obj.transform.parent = uiroot.transform;
                obj.transform.localScale = Vector3.one;
                obj.transform.localPosition = Vector3.zero;
                instance = obj.GetComponent<LoadingPanel>();
            }
            return instance;
        }
    }


    public UISlider slider;
    public bool isLoading;
    public UILabel tipWordLabel;
    private bool autoSlider = true;
    //private float sliderSpeed = 0.01f;
    //private float sliderTime = 3f;
    private float sliderTime1 = 20f;
    private float sliderTime2 = 25f;
    private float sliderTime3 = 45f;
    private float sliderTime4 = 60f;
    private float sliderTime5 = 300f;
    //private string defaultTip = "defaultTip";
    
    #region loading or mask
    public GameObject loading;
    public GameObject masking;
    public Renderer maskRender;
    public GameObject bottomLoading;
    public GameObject point1;
    public GameObject point2;
    public GameObject point3;
    //public UIPanel maskPanel;
    public int maskDir = 0;
    private float duration;
    //UIPanel panelAlpha;
    int showNum = 1;
    #endregion


    public void SetTipWord(string tipWord)
    {
        tipWordLabel.text = tipWord;
    }

    public void ShowLoadingPre()
    {
        ShowLoading();
        slider.gameObject.SetActive(false);
    }

    public void ShowLoading(string tipWord = null, bool autoSlider = true,float sliderTime = 3f)
    {
		if (!LoadingKit.isLoadingKit) {
			Debug.Log ("show mask ui");
			UIAutoSize.Instance.ShowMaskUI (true);
		}

        maskDir = 0;
        gameObject.SetActive(true);

        loading.SetActive(true);
        masking.SetActive(false);
        bottomLoading.SetActive(false);
        this.autoSlider = autoSlider;
        slider.gameObject.SetActive(true);
        slider.value = 0;
        
        if (tipWord == null)
        {
            tipWord = Dict.Blo.DictTipWordBlo.GetRandomTip();
        }
        if (tipWord == null)
        {
            tipWordLabel.transform.parent.gameObject.SetActive(false);
        }
        else
        {
            tipWordLabel.text = tipWord;
            tipWordLabel.transform.parent.gameObject.SetActive(true);
        }
        //this.sliderTime = sliderTime;
        isLoading = true;
    }

    public void ShowMask(float time) {
        gameObject.SetActive(true);
        loading.SetActive(false);
        masking.SetActive(true);
        bottomLoading.SetActive(true);
        maskDir = 1;
        duration = time;
        maskRender.material.color = new Color(1,1,1,0);
        //maskPanel.alpha = 0;
        StartCoroutine(pointShow(0.3f));
    }

    public void ShowMask()
    {
        gameObject.SetActive(true);
        loading.SetActive(false);
        masking.SetActive(true);
        bottomLoading.SetActive(false);
        //panelAlpha = bottomLoading.GetComponent<UIPanel>();
        maskDir = 1;
        //maskPanel.alpha = 1;
        maskRender.material.color = new Color(1, 1, 1, 1);
        StartCoroutine(pointShow(0.3f));
    }


    public void HideMask(float time)
    {
        //Debug.LogError("Hide loading mask");
        StopAllCoroutines();
        loading.SetActive(false);
        masking.SetActive(true);
        bottomLoading.SetActive(false);
        //maskPanel.alpha = 1;
        maskRender.material.color = new Color(1, 1, 1, 1);
        maskDir = -1;
        duration = time;
        if(gameObject.activeSelf)
            StartCoroutine(_HideLoading(time));
    }

    IEnumerator _HideLoading(float time)
    {
        yield return new WaitForSeconds(time); 
        HideLoading();
    }

    public void HideLoading()
    {
        slider.value = 1;
        ManagerController.Instance.StartCoroutine(startHide(0.1f));
        //this.gameObject.SetActive(false);
        //isLoading = false;
        //if(UI.TownPage.isTownPage || Battle.BattleManager.isBattleUI)
        //    UIAutoSize.Instance.ShowMaskUI(false);
        //else
        //    UIAutoSize.Instance.ShowMaskUI(true);
    }
    IEnumerator startHide(float time)
    {
        yield return new WaitForSeconds(time); 
        this.gameObject.SetActive(false);
        isLoading = false;
        if (UI.TownPage.isTownPage ||
            Battle.BattleManager.isBattleUI || 
            UI.UICopyRunMap.isUICopyRunMap || 
            UI.UIFieldRunMap.isUIFieldRunMap||
            UI.UIUnionRunMap.isUIUnionRunMap ||
            UI.UIUnionFightRunMap.isUnionFightPage ||
            LoadingKit.isLoadingKit)

            UIAutoSize.Instance.ShowMaskUI(false);
        else
            UIAutoSize.Instance.ShowMaskUI(true);
    }

    public void HideDelay(float delayTime)
    {
        if(gameObject.activeSelf)
        {
            StartCoroutine(_HideDelay(delayTime));
        }
    }

    public IEnumerator _HideDelay(float delayTime)
    {
        yield return new WaitForSeconds(delayTime);
        HideLoading();
    }

    public void SetPercent(float percent)
    {
        slider.value = percent;
    }

    private void Update()
    {
        if (autoSlider && slider.value <= 0.1f)
        {
            slider.value += Time.smoothDeltaTime / sliderTime1;
        }
        else if (autoSlider && slider.value > 0.1f && slider.value <= 0.3f)
        {
            slider.value += Time.smoothDeltaTime / sliderTime2;
        }
        else if (autoSlider && slider.value > 0.3f && slider.value <= 0.6f)
        {
            slider.value += Time.smoothDeltaTime / sliderTime3;
        }
        else if (autoSlider && slider.value > 0.6f && slider.value <= 0.9f)
        {
            slider.value += Time.smoothDeltaTime / sliderTime4;
        }
        else if (autoSlider && slider.value > 0.9f && slider.value < 0.97f)
        {
            slider.value += Time.smoothDeltaTime / sliderTime5;
        }
    }
    void LateUpdate()
    {
        float a = maskRender.material.color.a;
        if (maskDir == 1)
        {
            if (duration != 0)
            {
                a += Time.deltaTime * 1 / duration;
                maskRender.material.color = new Color(1, 1, 1, a);
                //if (panelAlpha != null)
                //    panelAlpha.alpha = a;
            }
            else
            {
                maskRender.material.color = new Color(1, 1, 1, 1);
                //panelAlpha.alpha = 1;
            }
            //maskPanel.alpha += Time.deltaTime * 1 / duration;
        }
        else if (maskDir == -1)
        {
            if (duration != 0)
            {
                a -= Time.deltaTime * 1 / duration;
                maskRender.material.color = new Color(1, 1, 1, a);
                //if(panelAlpha != null)
                //    panelAlpha.alpha = a;
                
            }
            else
            {
                maskRender.material.color = new Color(1, 1, 1, 1);
                //panelAlpha.alpha = 1;
            }
            //maskPanel.alpha -= Time.deltaTime * 1 / duration;
        }
    }

    IEnumerator pointShow(float time)
    {
        while(true)
        {
            showPoint(showNum);
            yield return new WaitForSeconds(time);
            showNum++;
            if(showNum>3)
            {
                showNum = 1;
            }
        }
    }

    void showPoint(int num)
    {
        switch(num)
        {
            case 1:
                point1.SetActive(true);
                point2.SetActive(false);
                point3.SetActive(false);
                break;
            case 2:
                point1.SetActive(true);
                point2.SetActive(true);
                point3.SetActive(false);
                break;
            case 3:
                point1.SetActive(true);
                point2.SetActive(true);
                point3.SetActive(true);
                break;  
        }
    }
    //public void OnEnable()
    //{
    //    Debug.LogWarning("Loading panel enable ");
    //}

    //public void OnDisable()
    //{
    //    Debug.LogWarning("Loading panel disable ");
    //}
}

